//
//  PushParser.m
//  FootPrint
//
//  Created by 秦 东方 on 4/28/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "PushParser.h"
#import "MsgManager.h"

@implementation PushParser

-(void)reqPush:(long long)userId isNews:(BOOL)isNews isShopActivity:(BOOL)isShopA isWeather:(BOOL)isWeather filter:(NSString*)filter
{
    InfoQueryRequestMessage_Builder* builder = [InfoQueryRequestMessage builder];
    [builder setUserId:userId];
    [builder setIfNews:isNews];
    [builder setIfMerchantEvent:isShopA];
    [builder setIfWeather:isWeather];
    [builder setEventFilter:filter];
    NSData* queryData = [[builder build]data];
    
    if(queryData){
        
        [super noCacheRequestURL:URL_INFO_QUERY postData:queryData completion:^(NSData* resultData){
            @try {
                InfoQueryResponseMessage* resp = [InfoQueryResponseMessage parseFromData:resultData];
                [[MsgManager sharedInstance] mergeInfo:resp];
                
                NSMutableArray * retArr = [NSMutableArray arrayWithCapacity:8];
                [retArr addObjectsFromArray:[[MsgManager sharedInstance] pushMsgOfName:@"event"]];
                [retArr addObjectsFromArray:[[MsgManager sharedInstance] pushMsgOfName:@"invite"]];
                if (isShopA) {
                    [retArr addObjectsFromArray:[[MsgManager sharedInstance] pushMsgOfName:@"merchant"]];
                }
                if (isWeather) {
                    [retArr addObjectsFromArray:[[MsgManager sharedInstance] pushMsgOfName:@"url"]];
                }
                
                [_delegate didFinishPush:retArr];
            }
            @catch (NSException *exception) {
                [_delegate failPush];
            }
            
        } fail:^(NSError * err) {
            [_delegate failPush];
        }];
        
    } else {
        [_delegate failPush];
    }

}

@end
